package cn.ichiva.tushare.sdk.tushare;

import cn.ichiva.tushare.sdk.tushare.entity.TuRequest;
import cn.ichiva.tushare.sdk.tushare.entity.TuResponse;
import cn.ichiva.tushare.sdk.tushare.entity.TuTable;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 读取数据
 */
@Slf4j
public abstract class AbstractTuData implements TuData {

    private final TuClient client;

    private TuRequest request;

    public AbstractTuData(TuClient client){
        this.client = client;
    }

    public TuRequest getRequest() {
        return request;
    }

    public void setRequest(TuRequest request) {
        this.request = request;
    }

    @Override
    public Optional<TuTable> get() {
        log.info("读取服务器数据 api = {}",request.getApi());
        Optional<TuResponse> res = client.post(request);
        if (res.isPresent()) {
            TuTable data = res.get().getData();
            return Optional.ofNullable(data);
        }

        return Optional.empty();
    }
}
